package com.miniot.base.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.miniot.base.entity.Brand;
import com.miniot.base.service.BrandService;
import com.miniot.base.vo.BrandQueryVO;
import com.miniot.base.vo.BrandVO;
import com.miniot.base.dto.GetLodoDTO;
import com.miniot.business.base.BaseController;
import com.miniot.business.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 品牌表
 *
 * @author zzq
 * @date 2023-10-17 11:15:39
 */
@RestController
@AllArgsConstructor
@RequestMapping("/brand" )
@Api(value = "品牌表(后台)", tags = "品牌表-后台接口")

public class BrandController extends BaseController {

    private final BrandService brandService;

    /**
    * 分页查询
    * @param currentPage 当前页数
    * @param pageSize 每页数据量
    * @param brand 查询条件
    * @return 查询结果
    */
    @GetMapping("/listPage")
    @ApiOperation(value = "分页查询", notes = "传入分页信息与查询条件")
    public R<Page<Brand>> listBrandPage(
            @ApiParam(value = "当前页数", required = true, defaultValue = "1") @RequestParam Long currentPage,
            @ApiParam(value = "每页数据量", required = true, defaultValue = "10") @RequestParam Long pageSize,
            @ApiParam(value = "查询条件") BrandQueryVO brand) {
        Page<Brand> page = new Page<Brand>(currentPage, pageSize);
        return R.success(brandService.page(page, new LambdaQueryWrapper<Brand>()
                .like(ObjectUtil.isNotEmpty(brand.getBrandName()),Brand::getBrandName,brand.getBrandName())
                .orderByDesc(Brand::getCreateTime)),"分页信息查询成功");
    }


    /**
    * 通过id查询品牌表
    * @param id id
    * @return Result
    */
    @GetMapping("/getById")
    @ApiOperation(value = "通过id查询品牌表", notes = "传入id")
    public R<Brand> getBrandById(@ApiParam(value = "id", required = true) @RequestParam("id") Long id) {
        return R.success(brandService.getById(id));
    }

    /**
    * 新增品牌表
    * @param brand 品牌表
    * @return Result
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增品牌表", notes = "传入Brand数据")
    public R<Boolean> save(@RequestBody BrandVO brand) {
        Brand brand1 = new Brand();
        BeanUtil.copyProperties(brand,brand1);
        Assert.isTrue(brandService.save(brand1), "操作失败");
        return R.success(true,"新增成功");
    }

    /**
    * 修改品牌表
    * @param brand 品牌表
    * @return Result
    */
    @PostMapping("/updateById")
    @ApiOperation(value = "修改品牌表", notes = "传入Brand数据")
    public R<Boolean> updateById(@RequestBody BrandVO brand) {
        Brand brand1 = new Brand();
        BeanUtil.copyProperties(brand,brand1);
        Assert.isTrue(brandService.updateById(brand1), "修改失败");
        return R.success(true,"修改成功");
    }

    /**
    * 通过id删除品牌表
    * @param id id
    * @return Result
    */
    @GetMapping("/deleteById")
    @ApiOperation(value = "通过id删除品牌表", notes = "传入id")
    public R<Boolean> deleteById(
            @ApiParam(value = "id", required = true) @RequestParam("id") Long id) {
        Assert.isTrue(brandService.removeBrandById(id), "操作失败");
        return R.success(true,"删除成功");
    }

    /**
     * 查询所有品牌
     * @return Result
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询所有品牌")
    public R<List<Brand>> getAll() {
        return R.success(brandService.list(),"查询成功");
    }
}